package com.hxc.basicdata.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import cn.hutool.core.date.DateUtil;
import com.hxc.common.core.domain.AjaxResult;
import com.hxc.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxc.basicdata.mapper.TVillagerInfoMapper;
import com.hxc.basicdata.domain.TVillagerInfo;
import com.hxc.basicdata.service.ITVillagerInfoService;

/**
 * 村民信息Service业务层处理
 * 
 * @author hxc
 * @date 2021-08-11
 */
@Service
public class TVillagerInfoServiceImpl implements ITVillagerInfoService 
{
    @Autowired
    private TVillagerInfoMapper tVillagerInfoMapper;

    /**
     * 查询村民信息
     * 
     * @param id 村民信息主键
     * @return 村民信息
     */
    @Override
    public TVillagerInfo selectTVillagerInfoById(Long id)
    {
        return tVillagerInfoMapper.selectTVillagerInfoById(id);
    }

    /**
     * 查询村民信息列表
     * 
     * @param tVillagerInfo 村民信息
     * @return 村民信息
     */
    @Override
    public List<TVillagerInfo> selectTVillagerInfoList(TVillagerInfo tVillagerInfo)
    {
        return tVillagerInfoMapper.selectTVillagerInfoList(tVillagerInfo);
    }

    /**
     * 新增村民信息
     * 
     * @param tVillagerInfo 村民信息
     * @return 结果
     */
    @Override
    public int insertTVillagerInfo(TVillagerInfo tVillagerInfo)
    {
        tVillagerInfo.setCreateTime(DateUtils.getNowDate());
        return tVillagerInfoMapper.insertTVillagerInfo(tVillagerInfo);
    }

    /**
     * 修改村民信息
     * 
     * @param tVillagerInfo 村民信息
     * @return 结果
     */
    @Override
    public int updateTVillagerInfo(TVillagerInfo tVillagerInfo)
    {
        return tVillagerInfoMapper.updateTVillagerInfo(tVillagerInfo);
    }

    /**
     * 批量删除村民信息
     * 
     * @param ids 需要删除的村民信息主键
     * @return 结果
     */
    @Override
    public int deleteTVillagerInfoByIds(Long[] ids)
    {
        return tVillagerInfoMapper.deleteTVillagerInfoByIds(ids);
    }

    /**
     * 删除村民信息信息
     * 
     * @param id 村民信息主键
     * @return 结果
     */
    @Override
    public int deleteTVillagerInfoById(Long id)
    {
        return tVillagerInfoMapper.deleteTVillagerInfoById(id);
    }

    /**
     * 功能描述: 根据村民名称获取村民信息
     * @author yzj
     * @param tVillagerInfo
     */
    @Override
    public TVillagerInfo queryVillagerInfo(TVillagerInfo tVillagerInfo) {
        return tVillagerInfoMapper.queryVillagerInfo(tVillagerInfo);
    }

    /**
     *
     * @Author wsf
     * @Date 14:31 2021/8/25
     * @param
     **/
    @Override
    public AjaxResult queryAgeDistributeList(TVillagerInfo tVillagerInfo) {
        List<Map<String,Object>>  allList = new ArrayList();
        //婴儿期
        Map<String,Object> babyMap = new HashMap<>();
        babyMap.put("ageStage","0~6");
        babyMap.put("stageName","婴儿期");
        AtomicInteger babyBoy = new AtomicInteger();
        AtomicInteger babyGirl = new AtomicInteger();
        //少儿期
        Map<String,Object> juvenileMap = new HashMap<>();
        juvenileMap.put("ageStage","7~12");
        juvenileMap.put("stageName","少儿期");
        AtomicInteger juvenileBoy = new AtomicInteger();
        AtomicInteger juvenileGirl = new AtomicInteger();
        //青少年
        Map<String,Object> teenagersMap = new HashMap<>();
        teenagersMap.put("ageStage","13~17");
        teenagersMap.put("stageName","青少年");
        AtomicInteger teenagersBoy = new AtomicInteger();
        AtomicInteger teenagersGirl = new AtomicInteger();
        //青壮年
        Map<String,Object> youngAdultsMap = new HashMap<>();
        youngAdultsMap.put("ageStage","18~45");
        youngAdultsMap.put("stageName","青壮年");
        AtomicInteger youngAdultsBoy = new AtomicInteger();
        AtomicInteger youngAdultsGirl = new AtomicInteger();
        //中年
        Map<String,Object> middleAgeMap = new HashMap<>();
        middleAgeMap.put("ageStage","46~69");
        middleAgeMap.put("stageName","中年");
        AtomicInteger middleAgeBoy = new AtomicInteger();
        AtomicInteger middleAgeGirl = new AtomicInteger();
        //老年
        Map<String,Object> oldAgeMap = new HashMap<>();
        oldAgeMap.put("ageStage","69以上");
        oldAgeMap.put("stageName","老年");
        AtomicInteger oldAgeBoy = new AtomicInteger();
        AtomicInteger oldAgeGirl = new AtomicInteger();
        int thisYear = DateUtil.thisYear();
        List<TVillagerInfo> tVillagerInfoList = tVillagerInfoMapper.selectTVillagerInfoList(tVillagerInfo);
        tVillagerInfoList.forEach((var)->{
            if (StringUtils.isNotBlank(var.getBirthday().toString())){
                //年龄计算
                int age = thisYear - Integer.parseInt(DateUtil.format(var.getBirthday(), "yyyy"));
                //婴儿期
                if (age>=0 && age<=6){
                    //男
                    if (var.getSex() == 0){
                         babyBoy.getAndIncrement();
                    }else {
                      //女
                        babyGirl.getAndIncrement();
                    }
                }
                //少儿期
                if (age>=7 && age<=12){
                    //男
                    if (var.getSex() == 0){
                        juvenileBoy.getAndIncrement();
                    }else {
                      //女
                        juvenileGirl.getAndIncrement();
                    }
                }
                //青少年
                if (age>=13 && age<=17){
                    //男
                    if (var.getSex() == 0){
                        teenagersBoy.getAndIncrement();
                    }else {
                      //女
                        teenagersGirl.getAndIncrement();
                    }
                }
                //青壮年
                if (age>=18 && age<=45){
                    //男
                    if (var.getSex() == 0){
                        youngAdultsBoy.getAndIncrement();
                    }else {
                      //女
                        youngAdultsGirl.getAndIncrement();
                    }
                }
                //中年
                if (age>=46 && age<=69){
                    //男
                    if (var.getSex() == 0){
                        middleAgeBoy.getAndIncrement();
                    }else {
                      //女
                        middleAgeGirl.getAndIncrement();
                    }
                }
                //老年
                if (age>69){
                    //男
                    if (var.getSex() == 0){
                        oldAgeBoy.getAndIncrement();
                    }else {
                      //女
                        oldAgeGirl.getAndIncrement();
                    }
                }
            }
        });
        //婴儿期
        babyMap.put("boy",babyBoy);
        babyMap.put("girl",babyGirl);
        allList.add(babyMap);
        //少儿期
        juvenileMap.put("boy",juvenileBoy);
        juvenileMap.put("girl",juvenileGirl);
        allList.add(juvenileMap);
        //青少年
        teenagersMap.put("boy",teenagersBoy);
        teenagersMap.put("girl",teenagersGirl);
        allList.add(teenagersMap);
        //青壮年
        youngAdultsMap.put("boy",youngAdultsBoy);
        youngAdultsMap.put("girl",youngAdultsGirl);
        allList.add(youngAdultsMap);
        //中年
        middleAgeMap.put("boy",middleAgeBoy);
        middleAgeMap.put("birl",middleAgeGirl);
        allList.add(middleAgeMap);
        //老年
        oldAgeMap.put("boy",oldAgeBoy);
        oldAgeMap.put("girl",oldAgeGirl);
        allList.add(oldAgeMap);
       return AjaxResult.success("success", allList);
    }

}
